package com.lfg.leetcode.leetcode0;

public class No24SwapPairs {
    public static ListNode swapPairs(ListNode head) {
        ListNode ret = null;
        ListNode lastNode = null;
        while (head != null) {
            ListNode node1 = head;
            ListNode node2 = node1.next;

            ListNode l = null;
            if (node2 != null) {
                ListNode tmp = node2.next;
                l = node2;
                node1.next = tmp;
                node2.next = node1;
            } else {
                l = node1;
            }
            if (ret == null) {
                ret = l;
            }
            if (lastNode != null) {
                lastNode.next = l;
            }
            lastNode = node1;
            head = lastNode.next;
        }
        return ret;

    }

    public static void main(String[] args) {
        ListNode l = swapPairs(ListNode.deserialize("[1,2,3,4]"));
        while (l != null) {
            System.out.println(l.val);
            l = l.next;
        }
    }
}

